From 4c593544d2ff39ba6631cc3fa0b91493973674d4 Mon Sep 17 00:00:00 2001
From: Philippe Gerum <rpm@xenomai.org>
Date: Fri, 14 Apr 2017 18:28:36 +0200
Subject: [PATCH] boilerplate: build obstack support conditionally

If the underlying *libc does not support obstacks, build a private
implementation, otherwise fully rely on the native support.

This fixes a long-standing issue building replacement code for the
native obstack support, which ended up breaking builds over uClibc
1.0.21 and later.  See https://patchwork.ozlabs.org/patch/745792/.

[Backport from upstream.]
Signed-off-by: Romain Naour <romain.naour@gmail.com>
---
 configure.ac                  |   4 ++
 include/boilerplate/obstack.h |  12 +++--
 lib/boilerplate/Makefile.am   |   5 +-
 lib/boilerplate/obstack.c     | 111 +++++++-----------------------------------
 4 files changed, 34 insertions(+), 98 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8222e1d..fbfb60f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -495,6 +495,10 @@ fi
 
 test x$CONFIG_XENO_VALGRIND_API = xy && AC_DEFINE(CONFIG_XENO_VALGRIND_API,1,[config])
 
+dnl Check for obstack support in *libc
+AC_CHECK_HEADERS(obstack.h,libc_has_obstack=y)
+AM_CONDITIONAL(XENO_PRIVATE_OBSTACK,[test x$libc_has_obstack = x])
+
 dnl Check for presence of some headers
 AC_CHECK_HEADERS(mqueue.h)
 
diff --git a/include/boilerplate/obstack.h b/include/boilerplate/obstack.h
index 206fe55..95eb792 100644
--- a/include/boilerplate/obstack.h
+++ b/include/boilerplate/obstack.h
@@ -103,8 +103,12 @@ Summary:
 
 /* Don't do the contents of this file more than once.  */
 
-#ifndef _OBSTACK_H
-#define _OBSTACK_H 1
+#ifndef _BOILERPLATE_OBSTACK_H
+#define _BOILERPLATE_OBSTACK_H 1
+
+#ifdef HAVE_OBSTACK_H
+#include_next <obstack.h>
+#else
 
 #ifdef __cplusplus
 extern "C" {
@@ -506,4 +510,6 @@ __extension__								\
 }	/* C++ */
 #endif
 
-#endif /* obstack.h */
+#endif /* !HAVE_OBSTACK_H */
+
+#endif /* _BOILERPLATE_OBSTACK_H */
diff --git a/lib/boilerplate/Makefile.am b/lib/boilerplate/Makefile.am
index 9b8612d..d7e6324 100644
--- a/lib/boilerplate/Makefile.am
+++ b/lib/boilerplate/Makefile.am
@@ -6,10 +6,13 @@ libboilerplate_la_LIBADD = libversion.la libiniparser.la
 libboilerplate_la_SOURCES =	\
 	ancillaries.c		\
 	hash.c			\
-	obstack.c		\
 	setup.c			\
 	time.c
 
+if XENO_PRIVATE_OBSTACK
+libboilerplate_la_SOURCES += obstack.c
+endif
+
 if XENO_DEBUG
 libboilerplate_la_SOURCES += debug.c
 endif
diff --git a/lib/boilerplate/obstack.c b/lib/boilerplate/obstack.c
index 4c645b2..fab62ce 100644
--- a/lib/boilerplate/obstack.c
+++ b/lib/boilerplate/obstack.c
@@ -22,39 +22,11 @@
 #include <xeno_config.h>
 #include <boilerplate/obstack.h>
 
-/* NOTE BEFORE MODIFYING THIS FILE: This version number must be
-   incremented whenever callers compiled using an old obstack.h can no
-   longer properly call the functions in this obstack.c.  */
-#define OBSTACK_INTERFACE_VERSION 1
-
-/* Comment out all this code if we are using the GNU C Library, and are not
-   actually compiling the library itself, and the installed library
-   supports the same library interface we do.  This code is part of the GNU
-   C Library, but also included in many other GNU distributions.  Compiling
-   and linking in this code is a waste when using the GNU C library
-   (especially if it is a shared library).  Rather than having every GNU
-   program understand `configure --with-gnu-libc' and omit the object
-   files, it is simpler to just do this in the source for each such file.  */
-
-#include <stdio.h>		/* Random thing to get __GNU_LIBRARY__.  */
-#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
-# include <gnu-versions.h>
-# if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
-#  define ELIDE_CODE
-# endif
-#endif
+/* Use this obstack implementation unconditionally.  */
 
+#include <stdio.h>
 #include <stddef.h>
-
-#ifndef ELIDE_CODE
-
-
-# if HAVE_INTTYPES_H
-#  include <inttypes.h>
-# endif
-# if HAVE_STDINT_H || defined _LIBC
-#  include <stdint.h>
-# endif
+#include <stdint.h>
 
 /* Determine default alignment.  */
 union fooround
@@ -97,22 +69,7 @@ void (*obstack_alloc_failed_handler) (void) = print_and_abort;
 
 /* Exit value used when `print_and_abort' is used.  */
 # include <stdlib.h>
-# ifdef _LIBC
 int obstack_exit_failure = EXIT_FAILURE;
-# else
-#  include "exitfail.h"
-#  define obstack_exit_failure exit_failure
-# endif
-
-# ifdef _LIBC
-#  if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
-/* A looong time ago (before 1994, anyway; we're not sure) this global variable
-   was used by non-GNU-C macros to avoid multiple evaluation.  The GNU C
-   library still exports it because somebody might use it.  */
-struct obstack *_obstack_compat;
-compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
-#  endif
-# endif
 
 /* Define a macro that either calls functions with the traditional malloc/free
    calling interface, or calls functions with the mmalloc/mfree interface
@@ -148,7 +105,7 @@ _obstack_begin (struct obstack *h,
 		void *(*chunkfun) (long),
 		void (*freefun) (void *))
 {
-  register struct _obstack_chunk *chunk; /* points to new chunk */
+  struct _obstack_chunk *chunk; /* points to new chunk */
 
   if (alignment == 0)
     alignment = DEFAULT_ALIGNMENT;
@@ -195,7 +152,7 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment,
 		  void (*freefun) (void *, void *),
 		  void *arg)
 {
-  register struct _obstack_chunk *chunk; /* points to new chunk */
+  struct _obstack_chunk *chunk; /* points to new chunk */
 
   if (alignment == 0)
     alignment = DEFAULT_ALIGNMENT;
@@ -246,11 +203,11 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment,
 void
 _obstack_newchunk (struct obstack *h, int length)
 {
-  register struct _obstack_chunk *old_chunk = h->chunk;
-  register struct _obstack_chunk *new_chunk;
-  register long	new_size;
-  register long obj_size = h->next_free - h->object_base;
-  register long i;
+  struct _obstack_chunk *old_chunk = h->chunk;
+  struct _obstack_chunk *new_chunk;
+  long	new_size;
+  long obj_size = h->next_free - h->object_base;
+  long i;
   long already;
   char *object_base;
 
@@ -308,9 +265,6 @@ _obstack_newchunk (struct obstack *h, int length)
   /* The new chunk certainly contains no empty object yet.  */
   h->maybe_empty_object = 0;
 }
-# ifdef _LIBC
-libc_hidden_def (_obstack_newchunk)
-# endif
 
 /* Return nonzero if object OBJ has been allocated from obstack H.
    This is here for debugging.
@@ -323,8 +277,8 @@ int _obstack_allocated_p (struct obstack *h, void *obj);
 int
 _obstack_allocated_p (struct obstack *h, void *obj)
 {
-  register struct _obstack_chunk *lp;	/* below addr of any objects in this chunk */
-  register struct _obstack_chunk *plp;	/* point to previous chunk if any */
+  struct _obstack_chunk *lp;	/* below addr of any objects in this chunk */
+  struct _obstack_chunk *plp;	/* point to previous chunk if any */
 
   lp = (h)->chunk;
   /* We use >= rather than > since the object cannot be exactly at
@@ -346,8 +300,8 @@ _obstack_allocated_p (struct obstack *h, void *obj)
 void
 obstack_free (struct obstack *h, void *obj)
 {
-  register struct _obstack_chunk *lp;	/* below addr of any objects in this chunk */
-  register struct _obstack_chunk *plp;	/* point to previous chunk if any */
+  struct _obstack_chunk *lp;	/* below addr of any objects in this chunk */
+  struct _obstack_chunk *plp;	/* point to previous chunk if any */
 
   lp = h->chunk;
   /* We use >= because there cannot be an object at the beginning of a chunk.
@@ -373,17 +327,12 @@ obstack_free (struct obstack *h, void *obj)
     abort ();
 }
 
-# ifdef _LIBC
-/* Older versions of libc used a function _obstack_free intended to be
-   called by non-GCC compilers.  */
-strong_alias (obstack_free, _obstack_free)
-# endif
 
 int
 _obstack_memory_used (struct obstack *h)
 {
-  register struct _obstack_chunk* lp;
-  register int nbytes = 0;
+  struct _obstack_chunk* lp;
+  int nbytes = 0;
 
   for (lp = h->chunk; lp != 0; lp = lp->prev)
     {
@@ -393,26 +342,6 @@ _obstack_memory_used (struct obstack *h)
 }
 
 /* Define the error handler.  */
-# ifdef _LIBC
-#  include <libintl.h>
-# else
-#  include "gettext.h"
-# endif
-# ifndef _
-#  define _(msgid) gettext (msgid)
-# endif
-
-# ifdef _LIBC
-#  include <libio/iolibio.h>
-# endif
-
-# ifndef __attribute__
-/* This feature is available in gcc versions 2.5 and later.  */
-#  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
-#   define __attribute__(Spec) /* empty */
-#  endif
-# endif
-
 static void
 __attribute__ ((noreturn))
 print_and_abort (void)
@@ -422,12 +351,6 @@ print_and_abort (void)
      happen because the "memory exhausted" message appears in other places
      like this and the translation should be reused instead of creating
      a very similar string which requires a separate translation.  */
-# ifdef _LIBC
-  (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
-# else
-  fprintf (stderr, "%s\n", _("memory exhausted"));
-# endif
+  fprintf (stderr, "memory exhausted\n");
   exit (obstack_exit_failure);
 }
-
-#endif	/* !ELIDE_CODE */
-- 
2.9.3

